package cn.wolfcode.wolf2w.business.service.impl;

import cn.wolfcode.wolf2w.business.api.domain.Strategy;
import cn.wolfcode.wolf2w.business.api.domain.StrategyRank;
import cn.wolfcode.wolf2w.business.mapper.StrategyMapper;
import cn.wolfcode.wolf2w.business.mapper.StrategyRankMapper;
import cn.wolfcode.wolf2w.business.query.StrategyRankQuery;
import cn.wolfcode.wolf2w.business.service.IStrategyRankService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 攻略排行Service业务层处理
 * 
 * @author hyz
 * @date 2024-07-26
 */
@Service
@Transactional
public class StrategyRankServiceImpl extends ServiceImpl<StrategyRankMapper,StrategyRank> implements IStrategyRankService {

    @Autowired
    private StrategyMapper strategyMapper;

    @Override
    public IPage<StrategyRank> queryPage(StrategyRankQuery qo) {
        IPage<StrategyRank> page = new Page<>(qo.getCurrentPage(), qo.getPageSize());
        return lambdaQuery()
                .page(page);
    }

    @Override
    public List<StrategyRank> queryRanksByType(Integer type) {
        List<StrategyRank> list = lambdaQuery().inSql(StrategyRank::getStatisTime, "select MAX(statis_time) from ta_strategy_rank")
                .eq(StrategyRank::getType, type)
                .orderByDesc(StrategyRank::getStatisnum)
                .list();
        return list;
    }

    @Override
    public void updateStrategyRank(int type, Date date) {

//        LocalDateTime localnow = LocalDateTime.now();
//        ZoneId zoneId = ZoneId.systemDefault();
//        Instant instant = localnow.atZone(zoneId).toInstant();
//        Date now = Date.from(instant);

        System.err.println(date.toLocaleString());

        LambdaQueryWrapper<Strategy> qw = new LambdaQueryWrapper<>();
        if(type == 1){
            qw.eq(Strategy::getIsabroad,1);
            qw.orderByDesc(Strategy::getViewnum);
        }else if(type == 2){
            qw.eq(Strategy::getIsabroad,0);
            qw.orderByDesc(Strategy::getViewnum);
        }else if(type == 3){
            qw.orderByDesc(Strategy::getViewnum);
        }
        qw.last("limit 10");
        List<Strategy> strategyList = strategyMapper.selectList(qw);
        List<StrategyRank> ranks = new ArrayList<>();
        for (Strategy strategy : strategyList) {
            StrategyRank rank = new StrategyRank();
            rank.setDestId(strategy.getDestId());
            rank.setDestName(strategy.getDestName());
            rank.setStrategyId(strategy.getId());
            rank.setStrategyTitle(strategy.getTitle());
            rank.setType((long)type);
            rank.setStatisnum(strategy.getViewnum());
            rank.setStatisTime(date);
            ranks.add(rank);
        }
        saveBatch(ranks);
    }
}
